// 重新登录模块
// 网络请求模块
import { getIndex, login } from '@/network/index'
// 加密函数
import { strEnc } from '@/assets/js/des'
// 登录参数
const loginParams = {
  ul: 0,
  pl: 0,
  lt: '',
  jsessionId: '',
  sfCookie27: '',
  execution: '',
  rsa: '',
  clientId: null
}
// 登录函数，用于通行证失效时重新登录数字广大
const reLogin = () => {
  return new Promise(async (resolve, reject) => {
    console.log("调用了");
    // 1 判断当前状态是否可以重新登录
    let username = window.localStorage.getItem('gzhu-username')
    let password = window.localStorage.getItem('gzhu-password')
    if(username === null || password === null) {
      // 缓存中没有账号密码，用户需要重新登录
      reject()
    }
    loginParams.ul = username.length
    loginParams.pl = password.length
    // 2 获取登录参数
    const result = await getIndex()
    loginParams.lt = result.data.lt
    loginParams.jsessionId = result.data.jsessionId
    loginParams.sfCookie27 = result.data.sfCookie27
    loginParams.execution = result.data.execution
    loginParams.clientId = result.data.clientId
    // 3 加密账号密码
    let str = username + password + loginParams.lt
    let rsa = strEnc(str, '1', '2', '3')
    loginParams.rsa = rsa
    // 4 登录
    const result2 = await login(loginParams)
    let CASTGC = result2.data.CASTGC
    // 5 将CASTGC重新设到存储中
    window.localStorage.setItem('CASTGC', CASTGC)
    resolve(CASTGC)
  })
}

export default reLogin